//
//  SongListViewController.m
//  Temp
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 不二晶. All rights reserved.
//

#import "SongListViewController.h"
#import "DetailSongListViewController.h"
#import "UIImageView+WebCache.h"
#import "songListModel.h"
#import "songListCell.h"
#import "NetWorkingTool.h"
#import "HeadView.h"
#import "musicViewController.h"
#import "AVSingleton.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SongListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *songListCollectionView;
@property(nonatomic, retain)NSMutableArray *titleArr;
@property(nonatomic, retain)NSMutableDictionary *titleDic;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation SongListViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.titleArr = [NSMutableArray array];
        self.titleDic = [NSMutableDictionary dictionary];
        self.title = @"分类";
    }
    [self songListData];
    return self;
}
- (void)dealloc
{
    [_songListCollectionView release];
    [_titleArr release];
    [_titleDic release];
    [_activity release];
    [super dealloc];
}
// 处理数据
- (void)songListData{
    if ([AVSingleton shareSingleton].internetNum == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    } else {
    [NetWorkingTool netWorkingToolStr:@"http://fm.api.ttpod.com/channellist?image_type=240_200&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=50a40f05044a0e2462e11cbf7d6612844b6c0008&idfa=058C286B-AB2C-41E2-A7FE-B67B3496FBBC&utdid=VQKI/UR603ADANyHaLs5uRrS&alf=201200" block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [NSMutableArray array];
        arr = dic[@"data"];
        for (NSInteger i = 0; i < arr.count; i++) {
            songListModel *songList = [[songListModel alloc] init];
            [songList setValuesForKeysWithDictionary:arr[i]];
            if (![self.titleArr containsObject:songList.parentname]) {
                //把分类的标题加入到标题数组当中
                [self.titleArr addObject:songList.parentname];
                // 创建一个分类标题字典
                [self.titleDic setObject:[NSMutableArray array] forKey:songList.parentname];
            }
            // 把songList加入到标题字典当中
            [[self.titleDic objectForKey:songList.parentname] addObject:songList];
            [songList release];
        }
        [self.songListCollectionView reloadData];
        [_activity stopAnimating];
    }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    // 创建一个layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH/3.75, WIDTH/3.125);
    // 设置最小的行间距
    flowLayout.minimumLineSpacing = 20;
    // 设置最小的列间距
    flowLayout.minimumInteritemSpacing = 1;
    
    // 设置头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(100, 20);
    
    // 设置一个item边框, 上下左右的边距大小
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.songListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 49) collectionViewLayout:flowLayout];
    self.songListCollectionView.delegate = self;
    self.songListCollectionView.dataSource = self;
    self.songListCollectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.songListCollectionView];
    
#pragma mark collectionView只能采用注册的方式生成cell
    [self.songListCollectionView registerClass:[songListCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.songListCollectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    // 进度轮
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
    
    
    //添加左侧按钮, 实现返回
//    UIImage *image = [UIImage imageNamed:@"01"];
//    // 创建左button
//    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick:)] autorelease];
}
// 左侧按钮方法实现
//- (void)leftButtonClick:(UIButton *)bu{
//    musicViewController *musicVC = [[musicViewController alloc] init];
//    [self.navigationController pushViewController:musicVC animated:YES];
//    musicVC.navigationController.navigationBarHidden = YES;
//    [musicVC release];
//}

// 协议必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[self.titleDic objectForKey:self.titleArr[section]] count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.titleArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    songListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSArray *arr = [self.titleDic objectForKey:self.titleArr[indexPath.section]];
    songListModel *songList = [[songListModel alloc] init];
    songList = arr[indexPath.row];
    cell.songListLabel.text = songList.songlist_name;
    NSURL *url = [NSURL URLWithString:songList.small_pic_url];
    [cell.songListImageView sd_setImageWithURL:url];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    // 使用时必须设置头部和底部大小
    HeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
    headView.backgroundColor = [UIColor blackColor];
    headView.alpha = 0.5;
    headView.headTextLabel.text = self.titleArr[indexPath.section];
    return headView;
}
// 跳转到详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailSongListViewController *detailSongList = [[DetailSongListViewController alloc] init];
    
    NSArray *arr = [self.titleDic objectForKey:self.titleArr[indexPath.section]];
    songListModel *songList = [[songListModel alloc] init];
    songList = arr[indexPath.row];
    // 传值
    detailSongList.passSonglist_id = songList.songlist_id;
    detailSongList.passQuantity = songList.quantity;
    detailSongList.passSmall_pic_url = songList.small_pic_url;
    detailSongList.passSonglist_name = songList.songlist_name;

    [self.navigationController pushViewController:detailSongList animated:YES];
    [detailSongList release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
